package com.zros.modules.business.controller;


import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zros.common.api.vo.Result;
import com.zros.common.api.vo.Select;
import com.zros.common.exception.JeecgBootException;
import com.zros.modules.business.dto.QuerySelectDto;
import com.zros.modules.business.service.ISelectService;
import com.zros.modules.shiro.authc.util.JwtUtil;
import com.zros.modules.system.dto.UserInfo;
import com.zros.modules.system.service.IUserService;

/**
 * <p>
 * 下拉框查询 前端控制器
 * </p>
 *
 * @author shimiao
 * @since 2019-04-22
 */
@RestController
@RequestMapping("/business/query-select")
@PropertySource("classpath:config.properties")
public class SelectController {
	
	@Value("${authority.companyKey}")
	private String authorityCompanyKey;
	
	@Value("${authority.campusKey}")
	private String authorityCampusKey;
	
	@Value("${authority.selfKey}")
	private String authoritySelfKey;
	
	@Value("${org.campusKey}")
	private String campusKey;
	
	@Autowired
	private ISelectService selectService;
	
	@Autowired
	private IUserService userService;
	
	/**
	 *  获取当前用户
	 */
	UserInfo queryCurrentUser(HttpServletRequest request,String queryMethod)
	{
		UserInfo currentUser = new UserInfo();
		String userAccount = JwtUtil.getUserAccountByToken(request);
		currentUser = userService.queryUserInfoByAccount(userAccount,queryMethod);
		
		return currentUser;
	}
	
	/**
	 * 用户管理校区下拉框查询
	 */
	@ApiOperation(value = "用户管理校区下拉框查询")
	@RequiresAuthentication
	@RequestMapping(value = "/allOrganization", method = RequestMethod.GET)
	public Result<List<Select>> queryAllOrgSelect(HttpServletRequest request) {
		List<Select> orgSelectList = new ArrayList<Select>();
		Map<String, String> selectMap = new HashMap<String, String>();
		
		selectMap.put("queryAllOrg", "queryAllOrg");
		orgSelectList = selectService.queryOrgSelect(selectMap);
		
		return new Result<List<Select>>().success().put(orgSelectList);
	}
	
	/**
	 * 校区下拉框查询
	 */
	@ApiOperation(value = "校区下拉框查询")
	@RequiresAuthentication
	@RequestMapping(value = "/organization", method = RequestMethod.GET)
	public Result<List<Select>> queryOaOrgSelect(HttpServletRequest request,QuerySelectDto querySelectDto) {
		List<Select> orgSelectList = new ArrayList<Select>();
		Map<String, String> selectMap = new HashMap<String, String>();
		
		//获取当前用户
		UserInfo currentUser = queryCurrentUser(request,querySelectDto.getQueryMethod());
		//获取数据权限key
		String dataResourceKey = currentUser.getDataResourceKey();
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		//具有公司权限，查询所有校区
		if(authorityCompanyKey.equals(dataResourceKey))
		{
			selectMap.put("campusKey", campusKey);
			orgSelectList = selectService.queryOrgSelect(selectMap);
			
			//管理数据，需要在公共校区中添加数据时使用
			if("0".equals(querySelectDto.getQueryKey()))
			{
				//添加全部校区
				Select select = new Select();
				select.setLabel("公共校区");
				select.setValue("0");
				orgSelectList.add(0, select);
			}
			
		}
		//校区权限，查询用户所属校区（可能多个）
		else
		{
			selectMap.put("campusKey", campusKey);
			selectMap.put("userId", currentUser.getId());
			orgSelectList = selectService.queryOrgSelect(selectMap);
		}
		
		return new Result<List<Select>>().success().put(orgSelectList);
	}
	
	/*@ApiOperation(value = "线索校区下拉框查询")
	@RequestMapping(value = "/clueOrganization", method = RequestMethod.GET)
	public Result<List<Select>> queryOrgSelect(HttpServletRequest request) {
		
		//获取当前用户
		UserInfo currentUser = queryCurrentUser(request,"queryClueOrStatistic");
		//获取数据权限key
		String dataResourceKey = currentUser.getDataResourceKey();
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		List<Select> orgSelectList = new ArrayList<Select>();
		Map<String, String> selectMap = new HashMap<String, String>();
		
	    //具有公司权限，查询所有校区
		if(authorityCompanyKey.equals(dataResourceKey))
		{
			selectMap.put("campusKey", campusKey);
			orgSelectList = selectService.queryOrgSelect(selectMap);
		}
		//具有自身或者校区权限，查询用户所属校区（可能多个）
		else
		{
			selectMap.put("campusKey", campusKey);
			selectMap.put("userId", currentUser.getId());
			orgSelectList = selectService.queryOrgSelect(selectMap);
		}
    	
		return new Result<List<Select>>().success().put(orgSelectList);
	}*/
	
	/**
	 * 课程下拉框查询
	 * 根据查询id和查询key查询科系或者课程列表
	 * 查询总课程前查询科系，在根据科系查询总课程
	 * */
	@ApiOperation(value = "课程下拉框查询")
	@RequiresAuthentication
	@RequestMapping(value = "/course", method = RequestMethod.GET)
	public Result<List<Select>> queryCourseSelect(Long queryId,String queryKey) {
		List<Select> courseSelectList = new ArrayList<Select>();
  		
  		//若queryId为空，返回空集合
  		if(null == queryId)
  		{
  			return new Result<List<Select>>().success().put(courseSelectList);
  		}
		
    	courseSelectList = selectService.queryCourseSelectByOrgId(queryId,queryKey);
		return new Result<List<Select>>().success().put(courseSelectList);
	}
	
	/**
	 * 教师下拉框查询
	 * 根据校区id和课程id查询老师列表
	 * */
	@ApiOperation(value = "教师下拉框查询")
	@RequiresAuthentication
	@RequestMapping(value = "/teacher", method = RequestMethod.GET)
	public Result<List<Select>> queryTeacherSelect(Long orgId,Long courseId) {
    	Map<String, Long> selectMap = new HashMap<String, Long>(); 
    	selectMap.put("orgId", orgId);
    	selectMap.put("courseId", courseId);
    	
    	List<Select> teacherSelectList = selectService.queryTeacherSelect(selectMap);
		return new Result<List<Select>>().success().put(teacherSelectList);
	}
    
	/**
	 * 咨询师(用户)下拉框查询
	 * 根据校区id查询咨询师(用户)
	 * */
	@ApiOperation(value = "咨询师(用户)下拉框查询")
	@RequiresAuthentication
  	@RequestMapping(value = "/user", method = RequestMethod.GET)
  	public Result<List<Select>> queryUserSelect(HttpServletRequest request,Long orgId) {
  		
  		//获取当前用户
		UserInfo currentUser = queryCurrentUser(request,"distributionClue");
		//获取数据权限key
		String dataResourceKey = currentUser.getDataResourceKey();
		if(StringUtils.isEmpty(dataResourceKey))
		{
			throw new JeecgBootException("数据权限查询不到！请联系管理员！");
		}
		
		//添加判断，如果分配权限为自身，只返回自身集合
		if(authoritySelfKey.equals(dataResourceKey))
		{
			List<Select> userSelectList = new ArrayList<Select>();
			Select select = new Select();
			select.setLabel(currentUser.getUserName());
			select.setValue(currentUser.getId());
			userSelectList.add(select);
			return new Result<List<Select>>().success().put(userSelectList);
		}
  		
      	List<Select> userSelectList = selectService.queryUserSelectByOrgId(orgId);
  		return new Result<List<Select>>().success().put(userSelectList);
  	}
    
    /* 
     * 
     * */
	
	/**
	 * 渠道下拉框查询
	 * 根据查询id和查询key查询渠道类型或者渠道
     * 查询总渠道前查询渠道类型，在根据类型查询总渠道
	 * */
	@ApiOperation(value = "渠道下拉框查询")
	@RequiresAuthentication
  	@RequestMapping(value = "/channel", method = RequestMethod.GET)
  	public Result<List<Select>> queryChannelSelect(Long queryId,String queryKey) {
  		List<Select> channelSelectList = new ArrayList<Select>();
  		
  		//若queryId为空，返回空集合
  		if(null == queryId)
  		{
  			return new Result<List<Select>>().success().put(channelSelectList);
  		}
  		
      	channelSelectList = selectService.queryChannelSelectByOrgId(queryId,queryKey);
  		return new Result<List<Select>>().success().put(channelSelectList);
  	}
  	
}
